package com.ittx.dzt.meione.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import com.ittx.dzt.edi.dto.InventoryUploadDTO;
import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import com.ittx.dzt.edi.dto.ShipmentDispatchDTO;
import com.ittx.dzt.edi.dto.StoreConfigDTO;
import com.ittx.dzt.meione.handler.AbstractBaseMeioneApiHandler;
import com.ittx.dzt.sys.response.Result;
import lombok.extern.slf4j.Slf4j;
import open.constant.PlatformApiConstant;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * 回传售后单拦截状态
 * <p>
 * <p>
 * /erp/un/sys/afs/notify  回传售后单拦截状态
 * </p>
 *
 * @author wl_code@163.com
 * @version 1.0
 * @date 2025/8/29
 */
@Slf4j
@Service
public class MeioneUploadReturnOrderInterceptStatusService extends AbstractBaseMeioneApiHandler<ShipmentDispatchDTO> {
	@Override
	public String getMeioneMethod() {
		return "/erp/un/sys/afs/notify";
	}

	@Override
	public String getLocalApiMethod() {
		return "";
	}

	@Override
	public Result<?> doProcess(PlatformApiParamDTO platformApiParamDTO, ShipmentDispatchDTO shipmentDispatchDTO) {
		StoreConfigDTO storeConfig = platformApiParamDTO.getStoreConfig();
		Map<String, Object> params = new HashMap<>();
		params.put("businessCode", storeConfig.getStoreUniqueCode());
		params.put("afsNo", shipmentDispatchDTO.getSourceOrderCode());
		// logisticsNo	String	F	SF000111222	快递单号（补发必填）
		// deliveryComCode	String	F	jtexpress	快递公司code（补发必填-见物流公司接口）
		Map<String, Object> extendMap = platformApiParamDTO.getExtendMap();
		if (CollUtil.isNotEmpty(extendMap)) {
			//其它拓展参数.
			// type	Number	T	0	0-拦截回传；1-收货回传；2-补发物流回传
			Optional.ofNullable(MapUtil.getBool(extendMap, "type"))
					.ifPresent(value -> params.put("type", value));
			// afsNo	String	T	202310300000001211	售后单号
			Optional.ofNullable(MapUtil.getBool(extendMap, "afsNo"))
					.ifPresent(value -> params.put("afsNo", value));
			// status	Number	T	0	0-拦截/收货/补发物流 成功；1-拦截/收货 失败
			Optional.ofNullable(MapUtil.getLong(extendMap, "status"))
					.ifPresent(value -> params.put("status", value));
			Optional.ofNullable(MapUtil.getLong(extendMap, "store_id"))
					.ifPresent(value -> params.put("store_id", value));
			Optional.ofNullable(MapUtil.getLong(extendMap, "after_sale_address_id"))
					.ifPresent(value -> params.put("after_sale_address_id", value));
		}
		params.put("deliveryComCode", shipmentDispatchDTO.getCarrierCode());
		params.put("logisticsNo", shipmentDispatchDTO.getWaybillCode());


		return this.executeApi(platformApiParamDTO.getStoreConfig(), params, platformApiParamDTO.getRequestId());
	}


}
